#include "player.h"
#include "../core/scene.h"
#include "../affiliate/spriteAnima.h"
#include "../affiliate/collider.h"
#include "../stats/stats.h"
#include "../affiliate/text_label.h"
#include "../stats/timer.h"
void player::init()
{
    actor::init();
    max_speed_ = 500.0f;
    flashTimer = timer::createTimer(this,0.4f);
    flashTimer->start();
    idle_ = spriteAnima::createAnima(this,"./assets/sprite/ghost-idle.png",2.0f,"player idle");
    move_ = spriteAnima::createAnima(this,"./assets/sprite/ghost-move.png",2.0f,"player move");
    move_->setActive(false);
    collider_ = collider::createCollider(this,idle_->getSize()/2.0f,Type::CIRCLE,"player collider");
    stats_ = stats::createStats(this,"player",30.0f,100.0f,50.0f,5.0f);
    deathEffect_ = effect::createEffect(nullptr,"./assets/effect/1764.png",getWorldPosition(),2.0f);
    deathEffect_->name_ = "deathEffect";
    weapon_ = weaponLightning::createWLightning(this,1.0f,10.0f);
    //textLabel::createTextLabel(this,"player","./assets/font/VonwaonBitmap-16px.ttf",16);
    //removeChild(deathEffect_);
}

void player::clean()
{
    actor::clean();
}

void player::render()
{
    if(stats_->isInvincible()&&flashTimer->getProgress()<0.5f){
        return;
    }
    actor::render();
    //game_.drawBoundray(render_position - glm::vec2(20.0f),render_position + glm::vec2(20.0f),5.0f,{1.0f,0.0f,0.0f,0.5f});
}

void player::update(float dt)
{
    //SDL_Log("player update is_active_:%d",is_active_);
    actor::update(dt);
    keyboardInput();
    speed_*= 0.9f;
    checkState();
    move(dt);
    syncCamera();
    checkIsDead();
    //SDL_Log("player update is_active_:%d",is_active_);
}

bool player::handleEvents(SDL_Event &event)
{
    if(actor::handleEvents(event)) return true;
    return false;
}

void player::keyboardInput()
{
    auto key = SDL_GetKeyboardState(NULL);
    if (key[SDL_SCANCODE_W]){
        speed_.y = -max_speed_;
        //SDL_Log("W");
    }else if(key[SDL_SCANCODE_S]){
        speed_.y = max_speed_;
        //SDL_Log("S");
    }
    if(key[SDL_SCANCODE_A]){
        speed_.x = -max_speed_;
        //SDL_Log("A");
    }else if(key[SDL_SCANCODE_D]){
        speed_.x = max_speed_;
        //SDL_Log("D");
    }
}

void player::syncCamera()
{
    game_.getCurrentScene()->setCameraPosition(world_position - game_.getScreenSize() / 2.0f);
}

void player::checkState()
{
    if(speed_.x<0){
        move_->setFlip(true);
        idle_->setFlip(true);
    }else{
        move_->setFlip(false);
        idle_->setFlip(false);
    }
    bool newMoving = (glm::length(speed_) > 0.1f);
    if(newMoving != isMoving_){
        isMoving_ = newMoving;
        changeState(isMoving_);
    }
}

void player::changeState(bool isMoving)
{
    if(isMoving){
        move_->setActive(true);
        idle_->setActive(false);
        move_->setCurrnetFrame(idle_->getCurrnetFrame());
        move_->setFrameTime(idle_->getFrameTime());
    }else{
        move_->setActive(false);
        idle_->setActive(true);
        idle_->setCurrnetFrame(move_->getCurrnetFrame());
        idle_->setFrameTime(move_->getFrameTime());
    }
}

void player::checkIsDead()
{
    if(!stats_->isAlive()){
        deathEffect_->setWorldPosition(world_position);
        game_.getCurrentScene()->safeAddChild(deathEffect_);
        //deathEffect_->setActive(true);
        setActive(false);
        game_.playSound("assets/sound/female-scream-02-89290.mp3");
    }
}

void player::takeDamage(float damage)
{
    if(!stats_ ||stats_->isInvincible()) return;
    actor::takeDamage(damage);
    game_.playSound("assets/sound/hit-flesh-02-266309.mp3");
}
